/* 
 @description: 关于我们

 @author: 韦淇霰

 @update: 2021年9月6日22:55:35
 */
    const add= document.getElementById('add'),
    input =document.querySelector('input'),
    back = document.querySelector('.back'),
    submit = document.getElementById('submit'),
    closes = document.getElementById('closes'),
    search = document.querySelector('.Search'),
    title = document.getElementById('title'),
    content = document.getElementById('content'),
    origin = document.getElementById('origin'),
    table = document.getElementById('tab');
    let isEdit = 0;    
    getList();
        const sou = document.querySelector('#sou');
    //搜索
    function getList(keyname='') {
        $.ajax({
        url: `http://127.0.0.1:7001/api/aboutUs?keyname=${keyname}`,
        method: "GET",
        success: function(res) {
                for (var i = 0; i < res.data.length; i++) {
                    const tr = document.createElement('tr');
                    for (let data in res.data[i]) {
                    const td = document.createElement("td");
                        td.innerHTML = res.data[i][data]
                        tr.appendChild(td); 
                        if(data === 'us_time'){
                            let time_d = res.data[i].us_time;
                            var times = new Date(parseInt(time_d)).toLocaleString();
                            td.innerHTML=times;
                        }
                    } 
                    const lasttd = document.createElement('td');
                    lasttd.innerHTML  = `<button type='button'  name="updata" class="up"  id='${res.data[i].id}'><i class="fa fa-wrench" aria-hidden="true"></i> 修改</button>`
                    tr.appendChild(lasttd)
                    tr.id = res.data[i].id;
                    table.appendChild(tr);
                  
                  
                }
            }  
        }
    )}
   
    //编辑
    function EditCa(us_title,  us_content, us_release ,id){
        $.ajax({
        url:`http://127.0.0.1:7001/api/aboutUs/${id}`,
        method:"put",
        data:{
            us_title ,
            us_content ,
            us_release ,
            id
        },
        success:function(res){
            console.log(res);
            if(res.code === 0){
            window.location.reload();
            closeBack();  
            }else{
                alert(res.msg);
            }
        }
    })
    }
    
    //获取详情
    function getInfo(id){
        $.ajax({
        url:`http://127.0.0.1:7001/api/aboutUs/${id}`,
        method:"get",
        data:{
            id,
        },
        success:function(res){
            // console.log(res.data);
            const fwb=document.querySelector('.w-e-text');
            console.log(fwb);
            if(res.code === 0){
            title.value = res.data.us_title;
            fwb.innerHTML = res.data.us_content;
            origin.value = res.data.us_release;
            }else{
                alert(res.msg)
            }
            console.log(res);
        }
        
        })
    }
    let ida=0;
    //通过事件委托操作编辑
    table.onclick = function(e){
       if(e.target.className === 'up'){
            back.style.display = 'block';
            const id  = e.target.id;
               ida = id
            getInfo(id);
        }
    }
    //搜索按钮
    search.onclick = function(){
        getList(sou.value);
        console.log(sou.value);
        table.innerHTML = `<tr class="tab_a">
                                <th>ID</th>
                                <th>标题</th>
                                <th>内容</th>
                                <th>发布时间</th>
                                <th>发布来源</th>
                                <th>操作</th>
                            </tr>`
        //刷新页面
        // window.location.reload();
    }
    closes.onclick = function(){
        closeBack();
    }
    function closeBack(){
        title.value = '';
        content.value ='';
        origin.value ='';
        back.style.display = 'none';
    }
       //富文本
       const E = window.wangEditor
       const editor = new E('#content')
       editor.create()
       
    //添加与编辑提交按钮
    submit.onclick = function(){
    //    const a= editor.txt.text();
    const a = editor.txt.html();
       console.log(a);
       console.log(ida);
        EditCa(title.value,a,origin.value,ida);
        //  window.location.reload();
    }